/*
 * @Description: 
 * @Author: changqing
 * @Date: 2021-08-16 14:52:45
 * @LastEditTime: 2021-08-16 14:53:19
 * @LastEditors: changqing
 * @Usage: 
 */
const createIterator = (data) => {
  let nextIndex = 0;

  return {
    hasNext: () => nextIndex < data.length,
    next: () => data[nextIndex++]
  };
};

// 建立迭代器
const iterator = createIterator(['a', 'b', 'c', 'd']);
// 访问所有元素
while (iterator.hasNext()) {
  const value = iterator.next();
  console.log(value);
}
console.log('end');